﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using KeepAnimals.BLL;
using KeepAnimals.DAL;

namespace KeepAnimals.UI
{
    public partial class FormBoxOfDapartment : Form
    {
        public FormBoxOfDapartment(string id)
        {
            
            InitializeComponent();
            //MessageBox.Show(id.ToString());
            int idDepartment = Int32.Parse(id);
            var box = Box.GetBoxsByIdDepartment(idDepartment);
            LoadObject(box, panelContentBox);
        }

        private void userControlBackground1_Load(object sender, EventArgs e)
        {
            
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            
        }

        private void FormBoxOfDapartment_Load(object sender, EventArgs e)
        {
            
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            
        }

        public void LoadObject(List<Box> boxes, Panel panel)
        {
            int x = 0;
            int y = 0;
            const int NBOBJECTLIGNE = 4; //nombre d'object par ligne
            //const int WIDTHOBJECT = 50;
            int mainContainWidth = panel.Width;
            int taille = mainContainWidth / NBOBJECTLIGNE;


            double nbObjectTotal = boxes.Count; // nombre objet dans la base
            int compteurObject = 1;
            double result = nbObjectTotal / NBOBJECTLIGNE;
            int nbLigne = roundUp(result);

            for (int i = 0; i < nbLigne; i++)
            {


                for (int j = 1; j <= NBOBJECTLIGNE; j++)
                {
                    if (j <= NBOBJECTLIGNE && compteurObject <= nbObjectTotal)
                    {
                        foreach (var boxe in boxes)
                        {
                            compteurObject++;
                            var pix = new Button();

                            pix.Text = boxe.Id.ToString();
                            


                            //pix.BackColor = Color.Red;
                            pix.Width = taille;
                            pix.Height = taille;
                            pix.Left = x;
                            pix.Top = y;

                            x += taille;
                            string id = boxe.Id.ToString();
                            
                                pix.BackgroundImage = KeepAnimals.UI.Properties.Resources.ImgCageVide;
                            
                            if (boxe.Empty == Box.BOX_FULL)
                            {
                                pix.BackgroundImage = KeepAnimals.UI.Properties.Resources.ImgCageFull;
                            }
                            pix.BackgroundImageLayout = ImageLayout.Stretch;
                            
                            panel.Controls.Add(pix);
                            pix.Click += pictureBox_Click;

                        }

                    }
                    else
                    {
                        break;
                    }
                }
                x = 0;
                y += taille;
            }
        }

        public int roundUp(double nombre)
        {
            int res = 0;

            if (Convert.ToInt32(nombre) == nombre)
            {
                res = Convert.ToInt32(nombre);
            }
            else
            {
                res = Convert.ToInt32(nombre) + 1;
            }

            return res;
        }

        private void pictureBox_Click(object sender, EventArgs e)
        {
            var button = (Button)sender;
            int idButton = Convert.ToInt32(button.Text.ToString());
            var box = Box.GetBoxById(idButton);
            //MessageBox.Show(box.IdAnimal.ToString());
            //MessageBox.Show(idButton.ToString());

            //MessageBox.Show(box.Empty.ToString());
            if (box.Empty == Box.BOX_EMPTY)
            {
                MessageBox.Show("vide");
            }
            else
            {
                var animal = Animal.GetAnimalByIdBox(idButton);

                

                Label nomAnimal = new Label();
                nomAnimal.Text = "Nom : " + animal.Name;
                Label ageAnimal = new Label();
                ageAnimal.Text = animal.Age.ToString() + " an(s)";
                Label poidsAnimal = new Label();
                poidsAnimal.Text = "Poids : " + animal.Weight.ToString();
                Label santeAnimal = new Label();
                santeAnimal.Text = "Santé : " + animal.State;
                Label desciptionAnimal = new Label();
                desciptionAnimal.Text = "Description : " + animal.Description;



                Button btndog = new Button();
                if (animal.IdCategory == Animal.ANIMAL_CAT)
                {
                    btndog.BackgroundImage = KeepAnimals.UI.Properties.Resources.ImgCat1;
                }
                else
                {
                    btndog.BackgroundImage = KeepAnimals.UI.Properties.Resources.ImgDog1;
                }

                //btndog.BackgroundImage = KeepAnimals.UI.Properties.Resources.ImgVoirMap1;
                btndog.BackgroundImageLayout = ImageLayout.Stretch;
                //UserControlAnimalDog dog = new UserControlAnimalDog();
                btndog.Dock = DockStyle.Fill;

                panelContentObjectAnimalImg.Controls.Add(btndog);

                panelAnimalName.Controls.Add(nomAnimal);
                panelAnimalNameAge.Controls.Add(ageAnimal);
                panelAnimalWeight.Controls.Add(poidsAnimal);
                panelAnimalState.Controls.Add(santeAnimal);
                panelAnimalDescription.Controls.Add(desciptionAnimal);
            }

        }
        public void CheckFieldAnimal(Animal animal)
        {
            if (animal.Name == null)
            {
                animal.Name = "";
            }
        }
    }
}
